<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;
use Phinx\Db\Adapter\MysqlAdapter;

final class CreateDemandPoolReportTable extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function up()
    {
        $table = $this->table('demand_pool_report', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '需求池报表']);
        $table->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '主键ID'])
            ->addColumn('project_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '项目id'])
            ->addColumn('report_date', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '上报月份期'])
            ->addColumn('episode_code', 'string', ['default' => '', 'limit' => 64, 'comment' => '剧集code'])
            ->addColumn('step_category_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '环节ID'])
            ->addColumn('attribute', 'string', ['default' => '', 'limit' => 255, 'comment' => '实体属性'])
            ->addColumn('stage', 'integer', ['limit' => 255, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '所处阶段'])
            ->addColumn('entity_ids', 'text', ['null' => true, 'limit' => MysqlAdapter::TEXT_MEDIUM,'comment' => '实体IDs'])
            ->addColumn('link_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '关联ID（排期ID）'])
            ->addColumn('type', 'integer', ['limit' => 255, 'null' => false, 'signed' => false, 'default' => 1, 'comment' => '类型：1=统计个数 2=统计分钟 3=统计集数 4=统计月份--比例'])
            ->addColumn('created', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '创建时间'])

            ->addIndex(['project_id', 'step_category_id', 'report_date'], ['type' => 'normal', 'name' => 'project_report_category'])
            ->addIndex(['link_id', 'report_date', 'type'], ['type' => 'normal', 'name' => 'link_id_type'])
            ->addIndex(['episode_code', 'project_id', 'type'], ['type' => 'normal', 'name' => 'episode_project_id'])
            ->addIndex(['type', 'project_id', 'link_id'], ['type' => 'normal', 'name' => 'project_type']);
        // 创建表
        $table->create();
    }

    public function down()
    {
        $this->table('demand_pool_report')->drop()->save();
    }
}
